package com.jy.api.controller.clerk;

import com.jy.api.dao.EmployeeDao;
import com.jy.api.domain.Employee;
import com.jy.api.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lihu on 2017/11/17.
 */
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkCouponController {

    @Autowired
    CouponService couponService;
    @Autowired
    private EmployeeDao employeeDao;


    /**
     * 获取优惠卷信息
     *
     * @param storeId
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "coupon")
    public ResponseEntity coupon(@RequestParam(value = "storeId") String storeId,@RequestParam("userName")String userName,
                                 @PageableDefault(value = 10, sort = {"addTime"},
                                         direction = Sort.Direction.DESC) Pageable pageable) {
        Employee employee = employeeDao.findByStoreIdAndUsernameAndEnabled(storeId,userName,true);
        if (employee.isSendCouponEnabled()){
            return new ResponseEntity(couponService.getCoupon(storeId, pageable), HttpStatus.OK);
        }else {
            return ResponseEntity.badRequest().body("暂无权限，请联系管理员！");
        }

    }

    /**
     * 单张优惠卷信息获取
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/coupon")
    public ResponseEntity getCoupon(@PathVariable String id) {
        return new ResponseEntity(couponService.getFondId(id), HttpStatus.OK);
    }

}
